﻿#include "Pirhana.h"
//#include "Global.h"
#include "Resource.h"

Pirhana::Pirhana(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State) : Object(x, y, ID, Object_State)
{ 
	this->setIDObjType(ID);

	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\pirhana_32.png"), 125, 0);
	//_sprite->SetFrame(0,0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();
	 

	/*_x = _x + 32/2;
	_yOld = _y + 32;
	_y = _y + 3 ;*/


	_xTemp = x + 32/2;
	//_yOld = y - 32;
	_yOld = y - 64 - 8;

	_yTemp = y ;

	
	timeStop = 0;
	//_vy = 0.05;
	veloc_y = 0.05;


	this->obj_state = Object_State;//IS_IDLE;
}

Pirhana::~Pirhana()
{
	/*if (_sprite != NULL)
	{
		delete _sprite;
	}*/
}

 
 

void Pirhana::render(int cameraX, int cameraY)
{
	 if (this->obj_state == IS_ACTIVING)
	 {
		 //_sprite->Render(_x, _y);
		 p_sprite->render(_xTemp, _yTemp, cameraX, cameraY);
		 
	 }
	
}

OBJECT_TYPE Pirhana::getObjectType()
{
	return OBJECT_ENEMY;
}

//void Pirhana::ResetRect()
//{
//	/*_rectBond.left = _x;
//	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
//	_rectBond.top = _y;
//	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;*/
//
//	_rectBond.left = _xTemp;
//	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
//	_rectBond.top = _yTemp;
//	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
//}

void Pirhana::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		timeStop += gameTime;
		if (timeStop > 2000)
		{
			timeStop = 0;
			changeState(IS_ACTIVING);
		}
		
		break;
	case IS_ACTIVING:
		p_sprite->setFrame(0,2);
		 
		
		//_y += _vy * gameTime ;
		_yTemp += veloc_y * gameTime ;
		//pos_y += veloc_y * gameTime;

		/*if(_y< this->GetBond().bottom - 32-5)
			ChangeState(IS_MOVING);*/
		//if((_vy<0&&_y<_yOld-56)||(_vy>0&&_y>_yOld))				//Tới mức giới hạn
		//	ChangeState(IS_IDLE);

		//if((_vy<0 && _y<_yOld-(32 + 32/2 - 2)))				//Tới mức giới hạn
		//{
		//	_vy = -_vy;

		//}
		//if((_vy>0 && _y>_yOld))				//Tới mức giới hạn
		//{
		//	ChangeState(IS_IDLE);

		//}

		if((veloc_y > 0) && (_yTemp > _yOld + 64))				//Tới mức giới hạn
		{
			veloc_y = -veloc_y;//_vy = -_vy;

		}
		if((veloc_y < 0 && _yTemp < _yOld))				//Tới mức giới hạn
		{
			changeState(IS_IDLE);

		}

		//ResetRect();
		break;
	 
	}
	p_sprite->update(gameTime);
	
	 
		

		 
}

void Pirhana::changeState(int state)
{
	obj_state = state;
	switch(obj_state)
	{
	case IS_IDLE:
		timeStop = 0;
		break;
	case IS_ACTIVING:
		/*if (_y < _yOld)
		{
			_vy = 1.5;
		} 
		else
		{ 
			_vy = -1.5;
		}*/
		//_vy = -0.05;
		veloc_y = 0.05;
		break;
	case IS_REMOVE:
		break;
	}
}